﻿using Microsoft.AspNetCore.Identity;
using System.ComponentModel;

namespace DataSync.Application
{
    /// <summary>
    /// 系统服务接口
    /// </summary>
    public class SystemAppService : IDynamicApiController
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ISystemService _systemService;
        public SystemAppService(ISystemService systemService, IHttpContextAccessor httpContextAccessor)
        {
            _systemService = systemService;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 获取系统描述
        /// </summary>
        /// <returns></returns>
        public string GetDescription()
        {
            return _systemService.GetDescription();
        }
        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="account"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public Task<TokenOutput> CreateToken(string account, string passWord)
        {
            if (string.IsNullOrEmpty(account) || string.IsNullOrEmpty(passWord))
            {
                return Task.Run(() => new TokenOutput()
                {
                    Msg = "账号和密码是必填项！",
                    AccessToken = string.Empty,
                    RefreshToken = string.Empty,
                });
            }
            if (account != App.GetConfig<string>("LoginUser:Account") || passWord != App.GetConfig<string>("LoginUser:Password"))
            {
                return Task.Run(() => new TokenOutput()
                {
                    Msg = "账号或密码是不正确！",
                    AccessToken = string.Empty,
                    RefreshToken = string.Empty,
                });
            }

            return _systemService.GetToken();
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        public void Logout()
        {
            _httpContextAccessor.HttpContext.SignoutToSwagger();
        }
    }
}
